package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 检查项的服务提供者实现类
 * @author zhengjd
 * @date 2020/3/29 10:37
 */

@Service//(interfaceClass = CheckItemService.class)   //注册服务
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    CheckItemDao checkItemDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //分页插件：当前页码，每页数量
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<CheckItem> checkItems = checkItemDao.findPage(queryPageBean);
        PageInfo pageInfo = new PageInfo(checkItems);  //获取总的查询数量
        return new PageResult(pageInfo.getTotal(),checkItems);
    }

    /**
     * 添加检查项
     */
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    /**
     * 编辑检查项
     */
    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    /**
     * 删除检查项
     * @param id
     */
    @Override
    public void delete(Integer id) {
        int count = checkItemDao.findCountByCheckItemId(id);
        if (count>0){
            throw new RuntimeException(MessageConstant.ERROR_CHECKITEM_DELETE_FAIL);
        }
        checkItemDao.delete(id);
    }

    /**
     * 查询所有检查项
     */
    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }

    /**
     * 根据id查询检查项目
     */
    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }
}


